// /import core
// /commands 选区路径
/**
 * 选区路径
 * 
 * @function
 * @name baidu.editor.execCommand
 * @param {String}
 *            cmdName elementpath选区路径
 */
baidu.editor.plugins['elementpath'] = function() {

	var domUtils = baidu.editor.dom.domUtils, currentLevel, tagNames, dtd = baidu.editor.dom.dtd;

	baidu.editor.commands['elementpath'] = {
		execCommand : function(cmdName, level) {
			var me = this, start = tagNames[level], range = me.selection
					.getRange();
			me.currentSelectedArr
					&& domUtils.clearSelectedArr(me.currentSelectedArr);

			currentLevel = level * 1;
			if (dtd.$tableContent[start.tagName]) {
				switch (start.tagName) {
					case 'TD' :
						me.currentSelectedArr = [start];
						start.className = me.options.selectedTdClass;
						break;
					case 'TR' :
						var cells = start.cells;
						for (var i = 0, ti; ti = cells[i++];) {
							me.currentSelectedArr.push(ti);
							ti.className = me.options.selectedTdClass;
						}
						break;
					case 'TABLE' :
					case 'TBODY' :

						var rows = start.rows;
						for (var i = 0, ri; ri = rows[i++];) {
							cells = ri.cells;
							for (var j = 0, tj; tj = cells[j++];) {
								me.currentSelectedArr.push(tj);
								tj.className = me.options.selectedTdClass;
							}
						}

				}
				start = me.currentSelectedArr[0];
				if (domUtils.isEmptyNode(start)) {
					range.setStart(start, 0).setCursor()
				} else {
					range.selectNodeContents(start).select()
				}
			} else {
				range.selectNode(start).select()

			}
		},
		queryCommandValue : function() {
			var start = this.selection.getStart(), parents = domUtils
					.findParents(start, true),

			names = [];
			tagNames = parents;
			for (var i = 0, ci; ci = parents[i]; i++) {
				if (ci.nodeType == 3)
					continue;
				var name = ci.tagName.toLowerCase();
				if (name == 'img' && ci.getAttribute('anchorname')) {
					name = 'anchor'
				}
				names[i] = name;
				if (currentLevel == i) {
					currentLevel = -1;
					break;
				}
			}
			return names;
		}
	}

};
